1/5 build: measure coverage on source only via coverage run#120
Open
vokracko wants to merge 1 commit intojerry-git:masterfrom
Open
1/5 build: measure coverage on source only via coverage run#120vokracko wants to merge 1 commit intojerry-git:masterfrom
vokracko wants to merge 1 commit intojerry-git:masterfrom
Conversation
The previous setup relied on pytest-cov (``--cov pytest_split --cov tests`` in addopts) which has a long-standing limitation when measuring a pytest plugin: the plugin imports happen during pytest's plugin discovery, *before* pytest-cov starts tracing. Anything that runs at import time (class bodies, decorators, type aliases, enum definitions) is reported as uncovered even though it obviously executed - the plugin wouldn't load otherwise. The ``CoverageWarning: Module pytest_split was previously imported, but not measured`` made that explicit. The cookiecutter template the project was generated from worked around this by adding ``--cov tests`` to the addopts, inflating the denominator with 100%-covered test files so the average reached 90%. That pulled the number up but stopped reporting the actual source coverage. Switch to ``coverage run -m pytest`` followed by ``coverage report``. Running ``coverage`` from the outside means tracing starts before Python imports anything at all, so plugin import-time code is counted. Real source coverage jumps from ~74% to 99%; threshold goes from 90 to 95 to reflect that.
This was referenced Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First of five stacked PRs that fix #25 —
pytest-splitgroup memberships drifting across pytest collection orders, e.g. when parametrise is fed a hash-randomised iterable (set,frozenset,dict.keys()) underPYTHONHASHSEED=random. The stack churns committed.test_durationsfiles and CI group manifests with no real code change, and 0.8.0's earlier fix only coveredleast_duration; this stack extends the same guarantee toduration_based_chunksand tightens a few related concerns along the way.Motivation. Subsequent PRs reshape the test suite — consolidating per-attribute assertions, adjusting test counts when algorithm output changes. The current coverage setup makes those changes look like coverage regressions even though source coverage is unchanged. Fix the measurement first so later PRs get clean diffs.
The pytest-cov limitation.
pytest_splitis itself a pytest plugin. When pytest starts up, it loads installed plugins via entry points. So pytest does roughly:pytest_splitandpytest-covpytest_split(executes class bodies, decorators, enum definitions — anything at module top level)pytest-covfinishes initialising and starts tracingClass definitions like
TestGroup,AlgorithmBase,LeastDurationAlgorithmand theAlgorithmsenum all execute in step 3 — before coverage starts tracing in step 4. Coverage records them as "uncovered" even though they obviously ran (the plugin wouldn't work otherwise). That's what the warning is telling you:Translation: "I noticed
pytest_splitwas already loaded by the time I started watching, so I can't tell you what ran during its import."The reported missing lines for
algorithms.pywere exactly: import statements, theTestGroupNamedTuple,AlgorithmBaseclass body,LeastDurationAlgorithmclass def,DurationBasedChunksAlgorithmclass def, and theAlgorithmsenum. All run-at-import code. None actually untested.The cookiecutter workaround. The project was generated from wolt-python-package-cookiecutter, which added
--cov teststo the addopts. Test files are 100% covered by definition, so this inflated the denominator and pulled the combined average up to ~90%. The threshold of 90 was set against that inflated number — the actual source coverage was always ~74%.What this PR does. Switch from
pytest --cov pytest_splittocoverage run -m pytest+coverage report. Runningcoveragefrom the outside means tracing starts before Python imports anything, so plugin import-time code is counted. Real source coverage jumps from ~74% to 99% (the only missing lines now are an abstract method'spassand one untested branch inipynb_compatibility.py).--cov pytest_split --cov tests --cov-report term-missing --no-cov-on-failfromaddopts.[tool.coverage.run] source = ["pytest_split"]socoverage reportknows what to measure.coverage run -m pytestthencoverage report.fail_underto 95 (4-point buffer below 99%).Stack. Each PR is independent enough to review on its own; merge in order.
coverage runTestGroupdirectly → 2/5 refactor: assert TestGroup directly in algorithm tests #121duration_based_chunks— closes Splits invalid when collection order not deterministic #25 → 5/5 fix: stabilise duration_based_chunks across collection orders #124